package com.woniuxy.woniuhis.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniuxy.woniuhis.entity.Deposit;
import com.woniuxy.woniuhis.entity.Inpatient;
import com.woniuxy.woniuhis.entity.PayIn;
import com.woniuxy.woniuhis.service.DepositService;
import com.woniuxy.woniuhis.service.InpatientService;
import com.woniuxy.woniuhis.util.ResponseInfo;
import com.woniuxy.woniuhis.util.ResponseJson;
import com.woniuxy.woniuhis.util.UUIDUtil;

@Controller
@RequestMapping("/deposit")
public class DepositController {
	@Autowired
	DepositService depositService;
	@Autowired
	InpatientService inpatientService;

	@RequestMapping("/depositList")
	@ResponseBody
	/**
	 * 病人押金展示
	 */
	public ResponseJson<Inpatient> depositList(Integer page, Integer limit) {
		ResponseJson<Inpatient> responseJson = new ResponseJson<Inpatient>();
		try {
			Inpatient condition = new Inpatient();
			List<Inpatient> deposit = depositService.findInpatientDeposit(condition, null, null);
			responseJson.setCode(0);
			responseJson.setCount(deposit.size());
			responseJson.setData(deposit);
			return responseJson;
		} catch (Exception e) {
			e.printStackTrace();
			responseJson.setCode(500);
			responseJson.setMsg("系统维护中");
			return responseJson;
		}
	}

	@RequestMapping("/searchDepositList")
	@ResponseBody
	/**
	 * 查询病人押金详情
	 */
	public ResponseJson<Inpatient> searchDepositList(@RequestBody Inpatient inpatient) {
		ResponseJson<Inpatient> responseJson = new ResponseJson<Inpatient>();
		Date begindate = null;
		Date enddate = null;
		try {
			if (inpatient.getInpatientFamlilyName() != "" && inpatient.getInpatientFamlilyName() != null) {
				String[] split = inpatient.getInpatientFamlilyName().split(" - ");
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				begindate = sdf.parse(split[0]);
				enddate = sdf.parse(split[1]);
			}
			List<Inpatient> deposit = depositService.findInpatientDeposit(inpatient, begindate, enddate);
			responseJson.setCode(0);
			responseJson.setCount(deposit.size());
			responseJson.setData(deposit);
			return responseJson;
		} catch (Exception e) {
			e.printStackTrace();
			responseJson.setCode(500);
			responseJson.setMsg("系统维护中");
			return responseJson;
		}
	}

	@RequestMapping("/addDeposit")
	@ResponseBody
	/**
	 * 新增押金
	 */
	public ResponseInfo addDeposit(@RequestBody Inpatient inpatient) {
		ResponseInfo responseInfo = new ResponseInfo();
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Inpatient inpatientById = inpatientService.findInpatientById(inpatient.getInpatientId());
			// 住院表中没有则无法添加
			if (inpatientById == null) {
				responseInfo.setState(500);
				responseInfo.setMsg("需要先完成住院");
				return responseInfo;
			}
			Deposit depositById = depositService.findDepositByInpaientId(inpatient.getInpatientId());
			// 已经有押金记录则无法添加
			if (depositById != null) {
				responseInfo.setState(500);
				responseInfo.setMsg("该病人已有押金记录");
				return responseInfo;
			}
			Deposit deposit = new Deposit(UUIDUtil.uuid32(), inpatient,
					new BigDecimal(inpatient.getInpatientFamlilyTel()));
			PayIn payIn = new PayIn(UUIDUtil.uuid32(), new BigDecimal(inpatient.getInpatientFamlilyTel()),
					inpatient.getInpatientId(), sdf.parse(inpatient.getInpatientFamlilyName()),
					inpatient.getInpatientName(), null, "2");
			depositService.addDeposit(deposit, payIn);
			responseInfo.setState(200);
			responseInfo.setMsg("添加成功");
			return responseInfo;
		} catch (Exception e) {
			e.printStackTrace();
			responseInfo.setState(500);
			responseInfo.setMsg("系统维护中");
			return responseInfo;
		}
	}

	@RequestMapping("/updateDeposit")
	@ResponseBody
	/**
	 * 修改押金
	 */
	public ResponseInfo updateDeposit(@RequestBody Inpatient inpatient) {
		ResponseInfo responseInfo = new ResponseInfo();
		try {
			// 判断是充值还是扣费
			int money = Integer.parseInt(inpatient.getInpatientFamlilyTel());
			if (money < 0) {
				// 直接扣除押金，没有新增付款订单
				Deposit deposit = new Deposit(null, inpatient, new BigDecimal(inpatient.getInpatientFamlilyTel()));
				depositService.modifyDeposit(deposit);
				responseInfo.setState(200);
				responseInfo.setMsg("扣费成功");
				return responseInfo;
			} else if (money > 0) {
				// 充值的同时插入一条充值记录
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Deposit deposit = new Deposit(null, inpatient, new BigDecimal(inpatient.getInpatientFamlilyTel()));
				PayIn payIn = new PayIn(UUIDUtil.uuid32(), new BigDecimal(inpatient.getInpatientFamlilyTel()),
						inpatient.getInpatientId(), sdf.parse(inpatient.getInpatientFamlilyName()),
						inpatient.getInpatientName(), null, "2");
				depositService.modifyDepositAndPayIn(deposit, payIn);
				responseInfo.setState(200);
				responseInfo.setMsg("充值成功");
				return responseInfo;
			}
			responseInfo.setState(500);
			responseInfo.setMsg("请输入正确数字");
			return responseInfo;
		} catch (NumberFormatException e) {
			e.printStackTrace();
			responseInfo.setState(500);
			responseInfo.setMsg("请输入正确数字");
			return responseInfo;
		} catch (Exception e) {
			e.printStackTrace();
			responseInfo.setState(500);
			responseInfo.setMsg("系统维护中");
			return responseInfo;
		}
	}
}
